/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ventanas;

import clases.Config;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import clases.*;
import java.util.Date;

/**
 *
 * @author b3f0
 */

public class WTabla extends javax.swing.JFrame {
    clases.Tabla tabla = new clases.Tabla();
    Cheques cheque = new Cheques();
    /**
     * Creates new form WTabla
     */
    public WTabla() throws SQLException {
        initComponents();
        
        tablacheques.setModel(tabla.ActualizarTabla());
        tablacheques.repaint();
        grupo_orden.add(rID);
        grupo_orden.add(rVencimiento);
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        grupo_orden = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablacheques = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        rVencimiento = new javax.swing.JRadioButton();
        rID = new javax.swing.JRadioButton();
        bBuscar = new javax.swing.JButton();
        bModificar = new javax.swing.JButton();
        bEliminar = new javax.swing.JButton();
        bTransferencia = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Chequera Online");

        tablacheques.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tablacheques);

        jButton1.setText("Nuevo Cheque");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Actualizar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        rVencimiento.setText("Vencimiento");

        rID.setText("ID");

        bBuscar.setText("Buscar Cheque");
        bBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBuscarActionPerformed(evt);
            }
        });

        bModificar.setText("Modificar Cheque");
        bModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bModificarActionPerformed(evt);
            }
        });

        bEliminar.setText("Eliminar Cheque");
        bEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bEliminarActionPerformed(evt);
            }
        });

        bTransferencia.setText("Transferir");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1047, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(rVencimiento)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(rID)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bBuscar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bModificar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bEliminar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(bTransferencia)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rVencimiento)
                    .addComponent(rID))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1)
                    .addComponent(bBuscar)
                    .addComponent(bModificar)
                    .addComponent(bEliminar)
                    .addComponent(bTransferencia))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if (rVencimiento.isSelected())
        {
            Config.setOrdenar("fvencimiento");
            System.out.println(grupo_orden.getSelection());
        }
        if (rID.isSelected())
        {
            Config.setOrdenar("id");
            System.out.println(grupo_orden.getSelection());
        }
        try {
            // TODO add your handling code here:
            tablacheques.setModel(tabla.ActualizarTabla());
        } catch (SQLException ex) {
            Logger.getLogger(WTabla.class.getName()).log(Level.SEVERE, null, ex);
        }
        tablacheques.repaint();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        WPanel agregarCheque = new WPanel();
        agregarCheque.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void bBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBuscarActionPerformed
        WBusqueda buscar = new WBusqueda();
        buscar.setVisible(true);
    }//GEN-LAST:event_bBuscarActionPerformed

    private void bModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bModificarActionPerformed
        

        Object ide = tablacheques.getValueAt(tablacheques.getSelectedRow(), 0);
        Object nom = tablacheques.getValueAt(tablacheques.getSelectedRow(), 1);
        Object ape = tablacheques.getValueAt(tablacheques.getSelectedRow(), 2);
        Object num = tablacheques.getValueAt(tablacheques.getSelectedRow(), 3);
        Object cuit = tablacheques.getValueAt(tablacheques.getSelectedRow(), 4);
        Object mon = tablacheques.getValueAt(tablacheques.getSelectedRow(), 5);
        Date emi =  (Date) tablacheques.getValueAt(tablacheques.getSelectedRow(), 6);
        Object ven = tablacheques.getValueAt(tablacheques.getSelectedRow(), 7);
        
        
        WModificar modificar = new WModificar(ide,nom,ape,cuit,emi,ven,mon,num);
        modificar.setVisible(true);
    }//GEN-LAST:event_bModificarActionPerformed

    private void bEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bEliminarActionPerformed
        // TODO add your handling code here:
//        int dato = tablacheques.getSelectedRow();
        int ide =(int) tablacheques.getValueAt(tablacheques.getSelectedRow(), 0);
//        int ide = (int) dato;
//        System.out.println (ide);
        cheque.BorrarCheque(ide);
        try {
            tablacheques.setModel(tabla.ActualizarTabla());
        } catch (SQLException ex) {
            Logger.getLogger(WTabla.class.getName()).log(Level.SEVERE, null, ex);
        }
        tablacheques.repaint();
        
    }//GEN-LAST:event_bEliminarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(WTabla.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(WTabla.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(WTabla.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(WTabla.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new WTabla().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(WTabla.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bBuscar;
    private javax.swing.JButton bEliminar;
    private javax.swing.JButton bModificar;
    private javax.swing.JButton bTransferencia;
    private javax.swing.ButtonGroup grupo_orden;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton rID;
    private javax.swing.JRadioButton rVencimiento;
    private javax.swing.JTable tablacheques;
    // End of variables declaration//GEN-END:variables
}
